<?php


namespace App\Http\Controllers\Admin;


use App\Models\AdminUserLog;
use App\Models\AuthGroup;
use App\Models\AuthRule;
use App\Models\NewAdmin;
use App\Models\ShopUser;
use App\Services\AuthService;
use App\Services\CommonService;
use App\Services\SmsService;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

class AuthController extends BaseController
{
    /**
     * 获取菜单列表
     * @param Request $request
     * @return false|string
     */
    public function menuList(Request $request){

        if($this->userGroup['rules'] != AuthService::ADMIN_RULE){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'管理员账号可以操作');
        }


        if($this->userGroup['rules'] != AuthService::ADMIN_RULE && $this->userGroup['type'] != AuthService::MODEL_ADMIN){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'超级管理员账号可以操作');
        }
        $authRule = AuthRule::where(['is_delete'=>CommonService::IS_DELETE_NOT])->orderBy('sort_num','asc')->get();
        $authRule = $authRule?$authRule->toArray():[];
        foreach ($authRule as &$v){
            $v['type_name'] = AuthService::MOUDEL[$v['type']]['remarks'];
            $v['label'] = $v['title'];
        }
        $authRule = generateTree($authRule);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$authRule);
    }

    /**
     * 添加菜单
     * @param Request $request
     * @return false|string
     */
    public function addMenu(Request $request){
        if($this->userGroup['rules'] != AuthService::ADMIN_RULE && $this->userGroup['type'] != AuthService::MODEL_ADMIN){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'超级管理员账号可以操作');
        }
        $pid = $request->input('pid','0');
        $title = $request->input('title','');
        $path = $request->input('path','');
        $icon = $request->input('icon','');
        $status= $request->input('status','0');
        $controller = $request->input('controller','');
        $action = $request->input('action','');
        $type = $request->input('type','1');
        $sort_num = $request->input('sort_num','0');
        if(!$title && $path){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'参数错误');
        }
        $pid = $pid?$pid:0;
        $authRule = AuthRule::where([['title',$title],['is_delete',CommonService::IS_DELETE_NOT],['pid',$pid],['type',$type]])->first();
        if($authRule){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'菜单名称已存在');
        }

        $data = [
            'pid'=>$pid,
            'title'=>$title,
            'path'=>$path,
            'icon'=>$icon,
            'status'=>$status,
            'controller'=>$controller,
            'action'=>$action,
            'type'=>$type,
            'sort_num'=>$sort_num,
        ];
        $res = AuthRule::insertGetId($data);
        if($res){
            AdminUserLog::addLog($this->users['id'],'添加菜单',$request->all(),AdminUserLog::OPERATION_TYPE_AUTH_USER);
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'添加菜单成功');
        }
        return ReturnMsg(CommonService::HTTP_CODE_FAIL,'添加菜单失败');
    }

    /**
     * 修改菜单
     * @param Request $request
     * @return false|string
     */
    public function editMenu(Request $request){
        if($this->userGroup['rules'] != AuthService::ADMIN_RULE && $this->userGroup['type'] != AuthService::MODEL_ADMIN){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'超级管理员账号可以操作');
        }
        $id = $request->input('id','0');
        $pid = $request->input('pid','0');
        $title = $request->input('title','');
        $path = $request->input('path','');
        $icon = $request->input('icon','');
        $status= $request->input('status','0');
        $controller = $request->input('controller','');
        $action = $request->input('action','');
        $type = $request->input('type','1');
        $sort_num = $request->input('sort_num','0');
        if(!$title && $path){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'参数错误');
        }
        $authRule = AuthRule::where(['id'=>$id])->first();
        if(!$authRule){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'菜单不存在');
        }
        $authRule = AuthRule::where([['title',$title],['is_delete',CommonService::IS_DELETE_NOT],['pid',$pid],['type',$type]])->first();
        $authRule = $authRule?$authRule->toArray():[];
        if($authRule && $id != $authRule['id']){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'菜单名称已存在');
        }

        $data = [
            'pid'=>$pid,
            'title'=>$title,
            'path'=>$path,
            'icon'=>$icon,
            'status'=>$status,
            'controller'=>$controller,
            'action'=>$action,
            'type'=>$type,
            'sort_num'=>$sort_num,
        ];
        $res = AuthRule::where(['id'=>$id])->update($data);
        if($res){
            AdminUserLog::addLog($this->users['id'],'修改菜单',$request->all(),AdminUserLog::OPERATION_TYPE_AUTH_USER);
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'操作成功');
        }
        return ReturnMsg(CommonService::HTTP_CODE_FAIL,'操作失败');
    }

    /**
     * 删除菜单
     * @param Request $request
     */
    public function delMenu(Request $request){
        if($this->userGroup['rules'] != AuthService::ADMIN_RULE && $this->userGroup['type'] != AuthService::MODEL_ADMIN){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'超级管理员账号可以操作');
        }
        $id = $request->input('id','');
        if(!$id){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'参数错误');
        }
        $count = AuthRule::where(['pid'=>$id,'is_delete'=>CommonService::IS_DELETE_NOT])->count('id');
        if($count > 0){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'菜单有关联子菜单无法删除');
        }
        $data = [
            'is_delete'=>CommonService::IS_DELETE_YES,
        ];
        $res = AuthRule::where('id',$id)->update($data);
        if($res){
            AdminUserLog::addLog($this->users['id'],'删除菜单',$request->all(),AdminUserLog::OPERATION_TYPE_AUTH_USER);
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'删除菜单成功');
        }
        return ReturnMsg(CommonService::HTTP_CODE_FAIL,'删除菜单失败');
    }

    /**
     * 菜单禁用启用
     * @param Request $request
     * @return false|string
     */
    public function setMenu(Request $request){
        if($this->userGroup['rules'] != AuthService::ADMIN_RULE && $this->userGroup['type'] != AuthService::MODEL_ADMIN){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'超级管理员账号可以操作');
        }
        $id = $request->input('id','');
        $status = $request->input('status',1);
        if(!$id){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'参数错误');
        }
        $count = AuthRule::where(['pid'=>$id,'status'=>CommonService::USER_STATUS_YES])->count('id');
        if($count > 0){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'菜单有关联子菜单无法禁用');
        }
        $data = [
            'status'=>$status,
        ];
        $res = AuthRule::where('id',$id)->update($data);
        if($res){
            AdminUserLog::addLog($this->users['id'],'菜单禁用启用',$request->all(),AdminUserLog::OPERATION_TYPE_AUTH_USER);
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'操作成功');
        }
        return ReturnMsg(CommonService::HTTP_CODE_FAIL,'操作失败');
    }


    /**
     * 添加角色
     */
    public function addGroup(Request $request){
        if($this->userGroup['rules'] != AuthService::ADMIN_RULE){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'管理员账号可以操作');
        }
        $title = $request->input('title','');
        $rules = $request->input('rules','');
         if(!$title || !$rules){
             return ReturnMsg(CommonService::HTTP_CODE_FAIL,'参数错误');
         }

        $addRules = explode(',',$rules);
        $authRuleId = AuthRule::where([['is_delete',CommonService::IS_DELETE_NOT],['type',$this->userGroup['type']]])->pluck('id');
        $groupRules = $authRuleId?$authRuleId->toArray():[];

        $authRule = AuthRule::where(['is_delete'=>CommonService::IS_DELETE_NOT,])
            ->whereIn('id',$groupRules)
            ->select('id','title','type','path','icon','status','is_use')
            ->orderBy('sort_num','asc')
            ->get();
        $authRule = $authRule?$authRule->toArray():[];
        foreach ($authRule as $item){
            if($this->userGroup['rules'] != AuthService::ADMIN_RULE){
                if($item['is_use'] == 1 || $item['status'] == CommonService::USER_STATUS_NOT){
                    return ReturnMsg(CommonService::HTTP_CODE_FAIL,'添加数据中包含禁用菜单');
                }
            }
        }

        $checkRes = AuthService::checkRuleId($addRules,$groupRules);
        if(!$checkRes){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'添加数据校验错误');
        }
        $data = [
            'title'=>$title,
            'rules'=>$rules,
            'create_time'=>dateFormat(time()),
            'type'=>$this->userGroup['type'],
            'pid'=>$this->userGroup['id'],
            'user_id'=>$this->users['id'],
        ];
        $res = AuthGroup::insertGetId($data);
        if($res){
            AdminUserLog::addLog($this->users['id'],'管理后台添加角色',$request->all(),AdminUserLog::OPERATION_TYPE_AUTH_USER);
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'添加角色成功');
        }
        return ReturnMsg(CommonService::HTTP_CODE_FAIL,'添加角色失败');
    }

    /**
     * 修改角色
     * @param Request $request
     * @return false|string
     */
    public function editGroup(Request $request){
        if($this->userGroup['rules'] != AuthService::ADMIN_RULE){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'管理员账号可以操作');
        }
        $id = $request->input('id','');
        $title = $request->input('title','');
        $rules = $request->input('rules','');
        if(!$id || !$title || !$rules){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'参数错误');
        }
        $selfAuthGroup  =   AuthGroup::where([['id',$id]])->select('type','rules')->first();
        $selfAuthGroup = $selfAuthGroup?$selfAuthGroup->toArray():[];
        if(!$selfAuthGroup){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'获取角色失败');
        }
        if($selfAuthGroup['rules'] == AuthService::ADMIN_RULE){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'超级管理员角色不可修改');
        }

        $addRules = explode(',',$rules);

        $authRuleId = AuthRule::where([['is_delete',CommonService::IS_DELETE_NOT],['type',$this->userGroup['type']]])->pluck('id');
        $groupRules = $authRuleId?$authRuleId->toArray():[];

        $authRule = AuthRule::where(['is_delete'=>CommonService::IS_DELETE_NOT,])
            ->whereIn('id',$groupRules)
            ->select('id','title','type','path','icon','status','is_use')
            ->orderBy('sort_num','asc')
            ->get();
        $authRule = $authRule?$authRule->toArray():[];
        foreach ($authRule as $item){
            if($this->userGroup['rules'] != AuthService::ADMIN_RULE){
                if($item['is_use'] == 1 || $item['status'] == CommonService::USER_STATUS_NOT){
                    return ReturnMsg(CommonService::HTTP_CODE_FAIL,'添加数据中包含禁用菜单');
                }
            }
        }

        $checkRes = AuthService::checkRuleId($addRules,$groupRules);
        if(!$checkRes){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'添加数据错误');
        }
        //角色递减删除操作
//        AuthService::checkRuleIdInc($addRules,$groupRules,$id);
        $data = [
            'title'=>$title,
            'rules'=>$rules,
        ];
        $res = AuthGroup::where('id',$id)->update($data);
        if($res){
            AdminUserLog::addLog($this->users['id'],'管理后台编辑角色',$request->all(),AdminUserLog::OPERATION_TYPE_AUTH_USER);
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'修改角色成功');
        }
        return ReturnMsg(CommonService::HTTP_CODE_FAIL,'修改角色失败');
    }


    /**
     * 角色列表
     * @param Request $request
     * @return false|string
     */
    public function groupList(Request $request){
        if($this->userGroup['rules'] != AuthService::ADMIN_RULE){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'管理员账号可以操作');
        }
        $status = $request->input('status','-100');
        $title = $request->input('title','');
        $page = $request->input('page',1);
        $pageSize = $request->input('pageSize',10);
        $where= [
//            ['user_id',$this->users['id']],
            ['type',$this->userGroup['type']],
            ['is_delete',CommonService::IS_DELETE_NOT],
        ];
        if($status>=CommonService::USER_STATUS_NOT){
            $where[] = ['status',$status];
        }
        if($title){
            $where[] = ['title','like','%'.$title.'%'];
        }

        $authRuleId = AuthGroup::where($where)
            ->select('id','title','create_time','update_time','status','rules')
            ->orderBy('create_time','desc')
            ->paginate($pageSize);
        $group = $authRuleId?$authRuleId->toArray():[];
//        $group = [];
//        $authRuleId = AuthGroup::where($where)->pluck('id');
//        $groupIds = $authRuleId?$authRuleId->toArray():[];
//        if($groupIds){
//            if($this->userGroup['rules'] == AuthService::ADMIN_RULE){
//                array_unshift($groupIds,$this->userGroup['id']);
//            }
//            $authRuleId = AuthGroup::whereIn('id',$groupIds)
//                ->select('id','title','create_time','update_time','status')
//                ->orderBy('create_time','desc')
//                ->paginate($pageSize);
//            $group = $authRuleId?$authRuleId->toArray():[];
//        }
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$group);
    }

    /**
     * 删除角色
     * @param Request $request
     */
    public function delGroup(Request $request){
        if($this->userGroup['rules'] != AuthService::ADMIN_RULE){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'管理员账号可以操作');
        }
        $id = $request->input('id','');
        if(!$id){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'参数错误');
        }
        $count = NewAdmin::where(['role_id'=>$id,'is_delete'=>CommonService::IS_DELETE_NOT])->count('id');
        if($count > 0){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'角色有关联账号无法删除');
        }
        $data = [
            'is_delete'=>CommonService::IS_DELETE_YES,
        ];
        $res = AuthGroup::where('id',$id)->update($data);
        if($res){
            AdminUserLog::addLog($this->users['id'],'管理后台删除角色',$request->all(),AdminUserLog::OPERATION_TYPE_AUTH_USER);
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'删除角色成功');
        }
        return ReturnMsg(CommonService::HTTP_CODE_FAIL,'删除角色失败');
    }

    /**
     * 角色禁用启用
     * @param Request $request
     */
    public function setGroup(Request $request){
        if($this->userGroup['rules'] != AuthService::ADMIN_RULE){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'管理员账号可以操作');
        }
        $id = $request->input('id','');
        $status = $request->input('status','1');
        if(!$id){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'参数错误');
        }

        $data = [
            'status'=>$status,
        ];
        $res = AuthGroup::where('id',$id)->update($data);
        if($res){
            AdminUserLog::addLog($this->users['id'],'管理后台角色禁用启用',$request->all(),AdminUserLog::OPERATION_TYPE_AUTH_USER);
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'操作成功');
        }
        return ReturnMsg(CommonService::HTTP_CODE_FAIL,'操作失败');
    }

    /**
     * 获取选择角色
     * @param Request $request
     * @return false|string
     */
    public function getGroupList(Request $request){
        if($this->userGroup['rules'] != AuthService::ADMIN_RULE){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'管理员账号可以操作');
        }
        $authRuleId = AuthGroup::where([
//            'user_id'=>$this->users['id'],
            'type'=>$this->userGroup['type'],
            'is_delete'=>CommonService::IS_DELETE_NOT,
            'status'=>CommonService::USER_STATUS_YES,
        ])
            ->select('id','title','rules')
            ->orderBy('create_time','desc')
            ->get();
        $group = $authRuleId?$authRuleId->toArray():[];
        foreach ($group as $k=>&$value){
            if($value['rules'] == AuthService::ADMIN_RULE){
                unset($group[$k]);
            }
        }
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$group);
    }


    /**
     * 获取用户菜单
     * @param Request $request
     * @return false|string
     */
    public function getAuthRule(Request $request){
        $handel = AuthRule::where(['is_delete'=>CommonService::IS_DELETE_NOT,'status'=>CommonService::USER_STATUS_YES,'type'=>$this->userGroup['type']]);
        $this->userGroup['rules'] != AuthService::ADMIN_RULE && $handel->whereIn('id',explode(',',$this->userGroup['rules']));
        $handel->select('id','title','type','path','icon','status','is_use','pid');
        $handel->orderBy('sort_num','asc');
        $authRule = $handel->get();
        $authRule = $authRule?$authRule->toArray():[];

        $menu = [];
        $ids = [];
        foreach ($authRule as $k=>&$item){
            $item['label'] = $item['title'];
            unset($item['title']);
            $menu[] = $item;
            if($item['is_use'] == 1){
                unset($authRule[$k]);
            }
            if(isset($authRule[$k]['id'])){
                $ids[] = $authRule[$k]['id'];
            }
        }
        $data['menu'] = generateTree($menu);
        $data['rule'] = generateTree($authRule);
        $data['ruleIds'] = $ids;
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$data);
    }

    /**
     * 根据角色id获取权限ids
     * @param Request $request
     * @return false|string
     */
    public function getAuthRuleById(Request $request){
        if($this->userGroup['rules'] != AuthService::ADMIN_RULE){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'管理员账号可以操作');
        }
        $groupId = $request->input('groupId','');
        if(!$groupId){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'参数错误');
        }
        $authGroup = AuthGroup::where(['id'=>$groupId])
            ->select('id','title','rules')
            ->first();
        $authGroup = $authGroup?$authGroup->toArray():[];
        $ruleIds = [];
        if($authGroup){
            $ruleIds = explode(',',$authGroup['rules']);
        }
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$ruleIds);
    }

    /**
     * 添加账户
     * @param Request $request
     * @return false|string
     */
    public function addUser(Request $request){
        if($this->userGroup['rules'] != AuthService::ADMIN_RULE){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'管理员账号可以操作');
        }
        $username = $request->input('username','');
        $mobile = $request->input('mobile','');
        $pwd = $request->input('pwd','');
        $role_id = $request->input('role_id','');
        $allow_ip = $request->input('allow_ip','');

        if(!$username || !$pwd || !$role_id || !$mobile){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'参数错误');
        }
        $checkMobile  = NewAdmin::where(['mobile'=>$mobile])->count();
        if($checkMobile > 0){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'手机号码已存在');
        }
        $data = [
            'username'=>$username,
            'password'=>md5($mobile.$pwd),
            'create_time'=>dateFormat(time()),
            'user_id'=>$this->users['user_id'],
            'role_id'=>$role_id,
            'mobile'=>$mobile,
            'allow_ip'=>$allow_ip
        ];
        $res = NewAdmin::insertGetId($data);
        if($res){
            AdminUserLog::addLog($this->users['id'],'管理后台添加账户',$request->all(),AdminUserLog::OPERATION_TYPE_AUTH_USER);
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'操作成功');
        }
        return ReturnMsg(CommonService::HTTP_CODE_FAIL,'操作失败');
    }

    /**
     * 编辑账户
     * @param Request $request
     * @return false|string
     */
    public function editUser(Request $request){
        if($this->userGroup['rules'] != AuthService::ADMIN_RULE){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'管理员账号可以操作');
        }
        $id = $request->input('id','');
        $username = $request->input('username','');
        $pwd = $request->input('pwd','');
        $role_id = $request->input('role_id','');
        $allow_ip = $request->input('allow_ip','');

        if(!$id ||!$username || !$role_id){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'参数错误');
        }
        $checkUser  = NewAdmin::where(['id'=>$id])->first();
        $checkUser = $checkUser?$checkUser->toArray():[];
        if(!$checkUser){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'禁止操作');
        }
        $data = [
            'username'=>$username,
            'role_id'=>$role_id,
            'allow_ip'=>$allow_ip
        ];

        if($pwd){
            $data['password'] = md5($checkUser['mobile'].$pwd);
        }


        $res = NewAdmin::where(['id'=>$id])->update($data);
        if($res){
            AdminUserLog::addLog($this->users['id'],'管理后台编辑账户',$request->all(),AdminUserLog::OPERATION_TYPE_AUTH_USER);
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'操作成功');
        }
        return ReturnMsg(CommonService::HTTP_CODE_FAIL,'操作失败');
    }

    /**
     * 获取所有平台商户列表
     * @return false|string
     */
    public function getMemberList(){
        if($this->userGroup['rules'] != AuthService::ADMIN_RULE){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'管理员账号可以操作');
        }
        $shop = ShopUser::select('id','shopname')->get();
        $shop = $shop?$shop->toArray():[];
        array_unshift($shop,['id'=>0,'shopname'=>'admin']);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$shop);
    }

    /**
     * 账号列表
     * @param Request $request
     * @return false|string
     */
    public function userList(Request $request){
        if($this->userGroup['rules'] != AuthService::ADMIN_RULE){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'管理员账号可以操作');
        }
        $status = $request->input('status','-100');
        $username = $request->input('username','');
        $mobile = $request->input('mobile','');
        $type = $request->input('type','');
        $userId = $request->input('userId','');
        $page = $request->input('page',1);
        $pageSize = $request->input('pageSize',10);
        $where= [
            ['admin.is_delete',CommonService::IS_DELETE_NOT],
        ];
        if($status>=CommonService::USER_STATUS_NOT){
            $where[] = ['admin.status',$status];
        }
        if($username){
            $where[] = ['admin.username','like','%'.$username.'%'];
        }
        if($mobile){
            $where[] = ['admin.mobile','like','%'.$mobile.'%'];
        }
        if($type){
            $where[] = ['admin.type',$type];
        }
        if($userId != ''){
            $where[] = ['admin.user_id',$userId==0?0:ShopUser::where('id',$userId)->value('member_id')];
        }


        $adminUser = NewAdmin::from('sp_other_admin as admin')
            ->where($where)
            ->leftjoin('sp_other_auth_group as group','admin.role_id','=','group.id')
            ->select('admin.id','admin.mobile','admin.username','admin.allow_ip','admin.create_time','admin.status','admin.type','admin.user_id','admin.role_id','group.title','group.rules')
            ->orderBy('admin.create_time','desc')
            ->paginate($pageSize);
        $adminUser = $adminUser?$adminUser->toArray():[];

        foreach ($adminUser['data'] as &$value){
            $value['type_name'] = AuthService::MOUDEL[$value['type']]['remarks'];
            $value['admin_name'] = $value['user_id'] == 0 ? 'admin' : ShopUser::where('member_id',$value['user_id'])->value('shopname');
            $log = AdminUserLog::where(['admin_id'=>$value['id'],'operation_type'=>AdminUserLog::OPERATION_TYPE_LOGIN])->orderBy('id','desc')->select('create_time','user_ip')->first();
            $log = obToArray($log);
            $value['login_time'] = isset($log['create_time']) ? $log['create_time'] : '';
            $value['login_ip'] = isset($log['user_ip']) ? $log['user_ip'] : '';
        }
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$adminUser);
    }

    /**
     * 批量操作设置状态
     * @param Request $request
     * @return false|string
     */
    public function setUser(Request $request){
        if($this->userGroup['rules'] != AuthService::ADMIN_RULE){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'管理员账号可以操作');
        }
        $ids = $request->input('ids','');
        $status = $request->input('status','');
        if(!$ids){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'参数错误');
        }
        $res = NewAdmin::whereIn('id',explode(',',$ids))
        ->update(['status'=>$status]);
        if($res){
            AdminUserLog::addLog($this->users['id'],'管理后台批量操作设置账号状态',$request->all(),AdminUserLog::OPERATION_TYPE_AUTH_USER);
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'操作成功');
        }
        return ReturnMsg(CommonService::HTTP_CODE_FAIL,'操作失败');
    }

    /**
     * 批量删除操作
     * @param Request $request
     * @return false|string
     */
    public function delUser(Request $request){
        if($this->userGroup['rules'] != AuthService::ADMIN_RULE){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'管理员账号可以操作');
        }
        $ids = $request->input('ids','');
        if(!$ids){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'参数错误');
        }
        $res = NewAdmin::whereIn('id',explode(',',$ids))
            ->update(['is_delete'=>CommonService::IS_DELETE_YES]);
        if($res){
            AdminUserLog::addLog($this->users['id'],'管理后台批量删除用户操作',$request->all(),AdminUserLog::OPERATION_TYPE_AUTH_USER);
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'操作成功');
        }
        return ReturnMsg(CommonService::HTTP_CODE_FAIL,'操作失败');
    }

    /**
     * 修改密码
     * @param Request $request
     * @return false|string
     */
    public function editPwd(Request $request){
        $code = $request->input('code','');
        $pwd = $request->input('pwd','');
        if(!$code || !$pwd){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'参数错误');
        }
        $checkCode = SmsService::checkCode($this->users['mobile'],$code);
        if(!$checkCode){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'验证码错误');
        }
        $data['password'] = md5($this->users['mobile'].$pwd);
        $res = NewAdmin::where(['id'=>$this->users['id']])->update($data);
        if($res){
            AdminUserLog::addLog($this->users['id'],'管理后台修改密码',$request->all(),AdminUserLog::OPERATION_TYPE_AUTH_USER);
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'操作成功');
        }
        return ReturnMsg(CommonService::HTTP_CODE_FAIL,'操作失败');
    }


}
